package com.yuwei.mine

import androidx.core.view.GravityCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.tencent.mmkv.MMKV
import com.yuwei.base.RoutePath
import com.yuwei.common.event.BusEvent
import com.yuwei.common.event.LiveDataBus
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.BaseViewFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.database.entity.AddressEntity
import com.yuwei.database.entity.FavVideoEntity
import com.yuwei.mine.adapter.FragmentAdapter
import com.yuwei.mine.adapter.SettingAdapter
import com.yuwei.mine.databinding.FragmentMineBinding
import com.yuwei.mine.entity.SettingEntity

@Route(path = RoutePath.MINE_FRAGMENT)
class MineFragment : BaseMviFragment<FragmentMineBinding,MineViewModel>() {
    lateinit var fragmentAdapter: FragmentAdapter
    var list= listOf<Fragment>()
    var listSetting= listOf<SettingEntity>()
    lateinit var settingAdapter: SettingAdapter
    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getFavAll"->{
                val tabs=ArrayList<String>()
                tabs.add("作品 "+"0")
                tabs.add("动态 "+"0")
                tabs.add("喜欢 ${(it.data as List<FavVideoEntity>).size}")

                TabLayoutMediator(binding.tab,binding.vp
                ) { tab, position -> tab.text = tabs[position] }.attach()
                val phone = MMKV.defaultMMKV().decodeString("phone")
                if (!phone.isNullOrEmpty()){
                    binding.tvName.text = phone
                }
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        vm.sendIntent(MineIntent.getFavAll)
        list= listOf(
            ARouter.getInstance().build(RoutePath.MODULE_MINE_WORKS_FRAGMENT).navigation() as Fragment,
            ARouter.getInstance().build(RoutePath.MODULE_MINE_DYNAMIC_FRAGMENT).navigation() as Fragment,
            ARouter.getInstance().build(RoutePath.MODULE_MINE_LIKES_FRAGMENT).navigation() as Fragment
        )
        fragmentAdapter=FragmentAdapter(requireActivity(),list)
        binding.vp.adapter=fragmentAdapter


        listSetting= listOf(
            SettingEntity(R.drawable.icon_shopping_car,"我的订单"),
            SettingEntity(R.drawable.icon_purse,"钱包"),
            SettingEntity(R.drawable.icon_link,"功能"),
            SettingEntity(R.drawable.icon_qr_code,"我的二维码"),
            SettingEntity(R.drawable.icon_star,"我的收藏"),
            SettingEntity(R.drawable.icon_picture,"私密作品"),
            SettingEntity(R.drawable.icon_line_chart,"创作者服务中心"),
            SettingEntity(R.drawable.icon_umbrella,"未成年保护工具"),
            SettingEntity(R.drawable.icon_setting,"设置"),
        )
        settingAdapter= SettingAdapter(listSetting)
        binding.rvMine.adapter=settingAdapter
        binding.rvMine.layoutManager=LinearLayoutManager(requireActivity())
        binding.setting.setOnClickListener {
            binding.dl.openDrawer(GravityCompat.END)
        }

    }

    override fun initData() {
        LiveDataBus.getDefault<BusEvent>("fav").observe(this@MineFragment){
            vm.sendIntent(MineIntent.getFavAll)
        }
    }

    override fun onResume() {
        super.onResume()

    }

}